package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop/api/request"
	"shop/initernal/dao"
	"shop/initernal/model"
)

func AddContext(c *gin.Context) {
	var req request.AddContext
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}

	context := model.Context{
		UserId:  c.GetUint("userId"),
		ShopId:  req.ShopId,
		Context: req.Context,
	}
	dao.Create(&context)

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "新增评论成功",
		"data":    context,
	})

}

func QueryAllContext(c *gin.Context) {
	var req request.GetInfoIdByShop
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	var data []model.Context
	if !dao.GetListByFields(&model.Context{ShopId: req.Id}, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "查询评论失败",
			"data":    nil,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "查询评论成功",
		"data":    data,
	})
}

func UpdateContext(c *gin.Context) {
	var req request.UpdateContext
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	ui := c.GetUint("userId")
	var data model.Context
	if !dao.GetOneByFields(&model.Context{ShopId: uint(req.ShopId), UserId: ui}, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    1000,
			"message": "评论不存在",
			"data":    nil,
		})
		return
	}

	if !dao.Update(&model.Context{Id: data.Id, Context: req.Context}) {
		c.JSON(http.StatusOK, gin.H{
			"code":    1000,
			"message": "评论修改失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "评论修改成功",
		"data":    data,
	})
}

//func DeleteContext(c *gin.Context) {
//	var req request.DeleteShop
//	err := c.ShouldBind(&req)
//	if err != nil {
//		c.JSON(http.StatusOK, gin.H{
//			"code":    10000,
//			"message": "取值失败",
//			"data":    err.Error(),
//		})
//		return
//	}
//
//	c.JSON(http.StatusOK, gin.H{
//		"code":    0,
//		"message": "商品删除成功",
//		"data":    nil,
//	})
//
//}
